/*
 * LRUMap.java
 *
 * Created on March 30, 2005, 1:39 PM
 */

package jm.lib.collections.map;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author Jiming Liu
 */
public class LRUMap<K,V> extends LinkedHashMap<K,V> implements Cloneable {
    
    private static final long serialVersionUID = 2132232975985428602L;
    
    private int threshold;
    
    
    /**
     * 
     * Same as LRUMap(128)
     *
     */
    public LRUMap(){
        super(16, 0.75f, true);
        this.threshold = 128;
    }
    
    public LRUMap(int threshold){
        super(16, 0.75f, true);
        this.threshold = threshold;
    }
    
    public LRUMap(int initialCapacity, int threshold){
        super(initialCapacity, 0.75f, true);
        this.threshold = threshold;
    }
    
    public LRUMap(int initialCapacity, float factor, int threshold){
        super(initialCapacity, factor, true);
        this.threshold = threshold;
    }
    
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return this.size() > this.threshold;
    }
    /*
    public void clear () {
        synchronized(this) {
            super.clear ();
        }
    }
     
    public V remove (Object key) {
        synchronized(this) {
            return super.remove (key);
        }
    }
     
    public V put (K key, V value) {
        synchronized(this) {
            return super.put (key, value);
        }
    }
     
    public void putAll (Map<? extends K, ? extends V> t) {
        synchronized(this) {
            super.putAll (t);
        }
    }
     */
    
    public int getThreshold(){
        return this.threshold;
    }
    
    public void setThreshold(int threshold){
        this.threshold = threshold;
    }
    
//    public Object clone() {
//        LRUMap<K,V> result = (LRUMap<K,V>)super.clone();
//        result.setThreshold(this.getThreshold());
//        return result;
//    }
    
    public int hashCode() {
        return super.hashCode() + this.threshold;
    }
    
    public boolean equals(Object o) {
        if(o == this) return true;
        if(!(o instanceof LRUMap)) return false;
        LRUMap obj = (LRUMap)o;
        if(this.getThreshold() != obj.getThreshold()) return false;
        return super.equals(o);
    }
    
    
}
